﻿using UnityEngine;
using System;


namespace UI
{
    public class GuideMask : MonoBehaviour
    {

        public GameObject gray_LeftTop;
        public GameObject gray_RightTop;
        public GameObject gray_LeftBottom;
        public GameObject gray_RightBottom;

        public GameObject alpha_LeftTop;
        public GameObject alpha_RightTop;
        public GameObject alpha_LeftBottom;
        public GameObject alpha_RightBottom;

        public GameObject leftHand;
        public GameObject rightHand;

        public UIWidget optionTarget;
        public UILabel dialogContentLeft;
        public UILabel dialogContentRight;
        public GameObject dialogLeft;
        public GameObject dialogRight;

        public GameObject currentHand { get; private set; }

        public GameObject[] highLigthParticleSize;

        public TweenAction tweenComponent;

        private Vector3 leftBottom, leftTop, rightBottom, rightTop;
        private GameObject target;
        public Action onClickCallback;

        public enum HandType
        {
            NONE = 0,
            LEFT = 1,
            RIGHT = 2,
        }
        public enum DialogType
        {
            NONE = 0,
            LEFT = 1,
            RIGHT = 2,
        }

        public enum AnchorType
        {
            Absolut = 0,
            LeftDown = 1,
            RightDown = 2,
            LeftUp = 3,
            RightUp = 4,
        }

        private const float TIME_ANIM = 0.5f;
        private const float TIME_WAIT = 0.6f;

        private float waitTime = 0;
        private bool playing = false;
        private Vector3 lastTargetPosition;
        

        private const int slowBackWidth = 1024;
        private const int slowBackHeight = 768;

        public void Hide()
        {
            gameObject.transform.localPosition = Vector3.zero;
            gameObject.SetActive(false);
            currentHand = null;
        }

        public void OnClickMask()
        {
            if (onClickCallback != null)
                onClickCallback();
            PlayMaskAnimation();
        }

        public void PlayMaskAnimation()
        {

            //gray_LeftTop.transform.localPosition = leftBottom + new Vector3(-slowBackWidth / 2, -slowBackHeight / 2, 0);
            //gray_RightTop.transform.localPosition = leftTop + new Vector3(-slowBackWidth / 2, slowBackHeight/2, 0);
            //gray_RightBottom.transform.localPosition = rightTop + new Vector3(slowBackWidth / 2, slowBackHeight / 2, 0);
            //gray_LeftBottom.transform.localPosition = rightBottom + new Vector3(slowBackWidth / 2, -slowBackHeight / 2, 0);

            //TweenActionMove alt = new TweenActionMove(gray_LeftTop.transform, leftBottom, TIME_ANIM, true);
            //TweenActionMove art = new TweenActionMove(gray_RightTop.transform, leftTop, TIME_ANIM, true);
            //TweenActionMove arb = new TweenActionMove(gray_RightBottom.transform, rightTop, TIME_ANIM, true);
            //TweenActionMove alb = new TweenActionMove(gray_LeftBottom.transform, rightBottom, TIME_ANIM, true);
            //TweenActionSpawn aspawn = new TweenActionSpawn(new TweenActionBase[] { alt, art, arb, alb });

            //tweenComponent.StopAllActions();
            //tweenComponent.RunAction(aspawn);

            //gray_LeftTop.SetActive(true);
            //gray_RightTop.SetActive(true);
            //gray_LeftBottom.SetActive(true);
            //gray_RightBottom.SetActive(true);

            //playing = true;
            //waitTime = 0;
        }

        void Update()
        {
            if(playing)
            {
                if(!tweenComponent.IsPlaying())
                {
                    playing = false;
                    waitTime = TIME_WAIT;
                }
            }
            if (waitTime>0)
            {
                waitTime -= Time.deltaTime;
                if (waitTime <= 0)
                {
                    gray_LeftTop.SetActive(false);
                    gray_RightTop.SetActive(false);
                    gray_LeftBottom.SetActive(false);
                    gray_RightBottom.SetActive(false);
                }
            }

            UpdateRect();

        }


        private void ShowHandType(HandType handType)
        {
            leftHand.SetActive(false);
            rightHand.SetActive(false);

            switch (handType)
            {
                case HandType.LEFT:
                    leftHand.SetActive(true);
                    currentHand = leftHand;
                    break;
                case HandType.RIGHT:
                    rightHand.SetActive(true);
                    currentHand = rightHand;
                    break;
            }
            if(handType==HandType.NONE)
            {
                optionTarget.gameObject.SetActive(false);
            }
            else
            {
                optionTarget.gameObject.SetActive(true);
            }
        }

        private void ShowParticle(int particleIndex)
        {
            foreach (GameObject go in highLigthParticleSize)
            {
                go.SetActive(false);
            }
            if (particleIndex >= 0)
            {
                highLigthParticleSize[particleIndex].SetActive(true);
            }
        }


        public void ShowForceWithWord(GameObject target,
            string text, 
            Vector3 textPos,
            DialogType dialogType,
            HandType handType)
        {
            gameObject.SetActive(true);

            this.target = target;
            //gameObject.transform.parent = target.transform;
            //gameObject.transform.localPosition = Vector3.zero;
            UpdateRect();

            ActiveGray(true);
            ActiveMask(true);
            
            switch(dialogType)
            {
                case DialogType.NONE:
                    dialogLeft.SetActive(false);
                    dialogRight.SetActive(false);
                    break;
                case DialogType.LEFT:
                    dialogLeft.SetActive(true);
                    dialogRight.SetActive(false);
                    dialogContentLeft.text = text;
                    dialogLeft.transform.localPosition = textPos;
                    break;
                case DialogType.RIGHT:
                    dialogLeft.SetActive(false);
                    dialogRight.SetActive(true);
                    dialogContentRight.text = text;
                    dialogRight.transform.localPosition = textPos;
                    break;
            }


            ShowHandType(handType);


        } 

        private void UpdateRect()
        {
            float border = 2f;
            if(target==null)
            {
                if (lastTargetPosition.Equals(Vector3.zero))
                    return;
                lastTargetPosition = Vector3.zero;
                leftBottom = Vector3.zero;
                rightTop = Vector3.zero;
                leftTop = Vector3.zero;
                rightBottom = Vector3.zero;
            }
            else
            {
                if (target.transform.position.Equals(lastTargetPosition))
                    return;
                lastTargetPosition = target.transform.position;
                var boxCollider = target.GetComponent<BoxCollider>();
                if(boxCollider!=null)
                {
                    leftBottom.x = boxCollider.center.x - boxCollider.size.x / 2+border;
                    leftBottom.y = boxCollider.center.y - boxCollider.size.y / 2+border;
                    rightTop.x = boxCollider.center.x + boxCollider.size.x / 2-border;
                    rightTop.y = boxCollider.center.y + boxCollider.size.y / 2-border;

                    leftBottom = target.transform.TransformPoint(leftBottom);
                    leftBottom = gameObject.transform.InverseTransformPoint(leftBottom);
                    rightTop = target.transform.TransformPoint(rightTop);
                    rightTop = gameObject.transform.InverseTransformPoint(rightTop);

                    leftTop = new Vector3(leftBottom.x, rightTop.y, 0);
                    rightBottom = new Vector3(rightTop.x, leftBottom.y, 0);
                }
                else
                {
                    var widget = target.GetComponent<UIWidget>();
                    if(widget!=null)
                    {
                        var corners = widget.worldCorners;
                        leftBottom = corners[0];
                        rightTop = corners[2];

                        leftBottom = gameObject.transform.InverseTransformPoint(leftBottom);
                        rightTop = gameObject.transform.InverseTransformPoint(rightTop);

                        leftBottom.x += border;
                        leftBottom.y += border;
                        rightTop.x -= border;
                        rightTop.y -= border;

                        leftTop = new Vector3(leftBottom.x, rightTop.y, 0);
                        rightBottom = new Vector3(rightTop.x, leftBottom.y, 0);
                    }
                }
                
            }

            ResetPosition();
        }

        public void ResetPosition()
        {
            leftBottom.z = 0;
            leftTop.z = 0;
            rightTop.z = 0;
            rightBottom.z = 0;

            alpha_LeftTop.transform.localPosition = new Vector3(leftBottom.x, leftBottom.y, 1);
            alpha_RightTop.transform.localPosition = new Vector3(leftTop.x, leftTop.y, 1);
            alpha_RightBottom.transform.localPosition = new Vector3(rightTop.x, rightTop.y, 1);
            alpha_LeftBottom.transform.localPosition = new Vector3(rightBottom.x, rightBottom.y, 1);

            gray_LeftTop.transform.localPosition = leftBottom;
            gray_RightTop.transform.localPosition = leftTop;
            gray_RightBottom.transform.localPosition = rightTop;
            gray_LeftBottom.transform.localPosition = rightBottom;


            optionTarget.transform.localPosition = (leftBottom + rightTop) / 2;
            optionTarget.width = (int)(rightBottom.x - leftBottom.x);
            optionTarget.height = (int)(leftTop.y - leftBottom.y);
            if (optionTarget.width % 2 != 0)
                optionTarget.width = optionTarget.width + 1;
            if (optionTarget.height % 2 != 0)
                optionTarget.height = optionTarget.height + 1;
        }

        public void ActiveMask(bool active)
        {
            alpha_LeftTop.SetActive(active);
            alpha_RightTop.SetActive(active);
            alpha_RightBottom.SetActive(active);
            alpha_LeftBottom.SetActive(active);
        }

        public void ActiveGray(bool active)
        {
            
            gray_LeftTop.SetActive(active);
            gray_RightTop.SetActive(active);
            gray_RightBottom.SetActive(active);
            gray_LeftBottom.SetActive(active);
        }
    }

}